const { HttpException } = require('../utils/http-exception')

async function exception(ctx, next) {
  try {
    await next()
  } catch (error) {
    console.log(error.message.indexOf('axFileSize exceeded'),888777);
    
    if(error instanceof HttpException){
      ctx.body = {
        message: error.message,
        code: error.code,
        requestUrl: `${ctx.method} ${ctx.path}`
      }
      ctx.status = error.status
    }else if(error.message.indexOf('axFileSize exceeded') != -1){
      ctx.body = {
        message: '图片尺寸太大。',
        code: '406',
        requestUrl: `${ctx.method} ${ctx.path}`
      }
      ctx.status = 500
    }else{
      //未知异常
      ctx.body = {
        message: error.message || '未知异常',
        code: 99999,
        requestUrl: `${ctx.method} ${ctx.path}`
      }
      ctx.status = 500
    }
  }
}

module.exports = exception